<?php

declare(strict_types=1);

namespace MonologMysql\Tests\Formatter;

use Hyperf\Database\Connection;
use Monolog\Logger;
use MonologMysql\Formatter\MysqlFormatter;
use MonologMysql\Handler\MysqlHandler;
use MonologMysql\Repository\HyperfRecordRepositoryImpl;
use MonologMysql\Tests\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class TestMysqlFormatter extends TestCase
{
    protected Logger $logger;

    public function setUp(): void
    {
        parent::setUp();
    }

    public function dpFormat(): array
    {
        return [
            [
                'channel',
                'this is a error message',
                [
                    'request_id' => '123',
                    'uri' => '/config/key',
                    'trace' => 'www.baidu.com'
                ],
                [
                    'request_id' => '123',
                    'channel' => 'channel',
                    'message' => 'this is a error message',
                    'level' => 200,
                    ''
                ]
            ]
        ];
    }

    /**
     * @dataProvider dpFormat
     * @param string $channel
     * @param string $message
     * @param array $context
     * @param $expected
     * @return void
     */
    public function testFormat(string $channel, string $message, array $context, $expected)
    {
        $logger = new Logger($channel);
        /** @var HyperfRecordRepositoryImpl|MockObject $hyperfRecordRepositoryImpl */
        $hyperfRecordRepositoryImpl = $this->getMockBuilder(HyperfRecordRepositoryImpl::class)
            ->onlyMethods(['insert'])
            ->disableOriginalConstructor()
            ->getMock();
        $hyperfRecordRepositoryImpl->expects($this->once())->method('insert')->with($this->arrayHasKey('request_id'));
        $handler = new MysqlHandler($hyperfRecordRepositoryImpl, Logger::DEBUG);
        $mysqlFormatter = new MysqlFormatter();
        $handler->setFormatter($mysqlFormatter);
        $logger->pushHandler($handler);
        $logger->info($message, $context);
    }

}